package com.itheima.service;

import com.itheima.common.R;
import com.itheima.mapper.DishMapper;
import com.itheima.mapper.SetmealDishesMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @author Envy
 */
@Service
public class SetmealService {
    @Resource
    SetmealMapper setmealMapper;
    @Resource
    DishMapper dishMapper;
    @Resource
    SetmealDishesMapper setmealDishesMapper;

    /**
     * 套餐分页查询
     */
    public R<SetmealPage> pageQuery(String name, Integer page, Integer pageSize) {
        List<SetmealPage> setmealPages = setmealMapper.pageQuery(name, page, pageSize);

        SetmealPage setmealPage = new SetmealPage();
        //设置所有页面的总条数
        setmealPage.setTotal(setmealMapper.count());
        //设置每页的显示数据条数
        setmealPage.setPageSize(pageSize);

        if (setmealPages != null) {
            //设置显示的当前页面
            setmealPage.setPage(page);
            //把查询到的所有数据放进SetmealPage对象的Records集合中
            setmealPage.setRecords(setmealPages);
            return R.success(setmealPage);
        }
        return R.error("失败");
    }

    /**
     * 删除套餐
     */

    public Boolean delete(List<String> ids) {
        for (String id : ids) {
            //根据id获取到Setmeal的对象
            Setmeal setmeal = setmealMapper.selectById(id);
            //根据Setmeal的对象获取到status(售卖状态)
            Integer status = setmeal.getStatus();
            //根据售卖状态判断是否可以删除
            if (status != 1) {
                setmealMapper.delet(id);
            } else {
                return false;
            }
        }
        return true;
    }

    /**
     * 新增套餐
     */
    public R<Object> addSetmeal(Setmeal setmeal) {
        //获取时间戳,将时间戳作为id
        long timeMillis = System.currentTimeMillis();
        String id = timeMillis + "";
        setmeal.setId(id);
        //获取到菜品内容添加到Dishlist集合中
        List<Dish> dishes = dishMapper.selectAll();
        setmeal.setDishlist(dishes);

        //获取setmealDishes集合
        List<SetmealDishes> setmealDishes2 = setmeal.getSetmealDishes();
        setmealMapper.addSetmeal(setmeal);

        if (setmealDishes2.size() != 0) {
            for (SetmealDishes setmealDishes : setmealDishes2) {
                Random random = new Random();
                int nextInt = random.nextInt(99999999);
                String id2 = nextInt + "";

                setmealDishes.setId(id2);
                setmealDishes.setSetmealId(id);
                setmealDishesMapper.addSetmealDishes(setmealDishes);
            }

        }
        return R.success(setmeal);
    }

    /**
     * 根据id获取套餐
     */
    public R<Setmeal> selectSetmeal(String id) {
        Setmeal setmeal = setmealMapper.selectSetmeal(id);
        List<SetmealDishes> setmealDishes = setmealDishesMapper.selectAll();

        if (setmealDishes != null) {
            setmeal.setSetmealDishes(setmealDishes);
        }
        if (setmeal != null) {
            return R.success(setmeal);
        }
        return R.error("查询失败");
    }

    /**
     * 修改套餐
     */
    public R<Object> updeteSetmeal(Setmeal setmeal) {
        //调用updete方法修改setmeal表
        setmealMapper.updeteSetmeal(setmeal);
        //在setmeal取出setmealdishes集合
        List<SetmealDishes> sd = setmeal.getSetmealDishes();
        //根据setmealid删除setmealdishes集合中所有关setmeal相关id的内容
        String id = setmeal.getId();
        setmealDishesMapper.delete(id);
        //遍历sd集合添加到setmealdishes表中
        Integer setmealDishes1 = null;
        for (SetmealDishes setmealDishes : sd) {
            //获取随机数作为套餐内容表的id
            Random random = new Random();
            int nextInt = random.nextInt(99999999);
            String id2 = nextInt + "";

            setmealDishes.setId(id2);
            setmealDishes.setSetmealId(setmeal.getId());
            setmealDishes1 = setmealDishesMapper.addSetmealDishes(setmealDishes);
        }
        if (setmealDishes1 != null) {
            return R.success("修改成功");
        }
        return R.error("失败");
    }

    /**
     * 套餐上架/下架
     */
    public R<Object> updatestatus(List<String> ids, Integer status) {
        for (String id : ids) {
            setmealMapper.updatestatus(id, status);
        }
        return R.success("操作成功");
    }
}
